/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PuriON;

import java.io.*;
import java.awt.*;
import java.net.*;
import javax.media.*;
import javax.media.protocol.*;
import javax.swing.*;
/**
 *
 * @author hyukm
 * ini
 */

public class PURION_Player extends JInternalFrame implements ControllerListener, Runnable {
    
    Player  player = null;
    float   rate = 1.0f;
    
    // 비디오가 플레이될 컴포넌트
    Component visualComponent = null;
    // 미디어에 대한 컨트롤(시작,정지 등.)을 제공하는 컴포넌트
    Component controlComponent = null;
    
    // 미디어 컴포넌트들이 위치할 패널
    JPanel pane = null;
    
    String url = null;
    
    
    boolean closed;
    boolean realized;
    boolean video;
    PURION_Player(String _u, boolean _v) {
        
        url = _u;
        closed = false;
        video = _v;
        this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE);        
        this.initComponents();
   }
    private void initComponents() {
        pane = new JPanel();
        pane.setLayout(new BorderLayout());        
        this.setContentPane(pane);
        this.setResizable(true);
        
        this.setMaximizable(true);
        this.setSize(400, 300);
        this.setLocation(300, 400);
        this.doLayout();
        //this.pack();
    }
    private DataSource getDataSource() {
        DataSource src = null;
        
        MediaLocator mrl = null;
        if ((mrl = new MediaLocator(url)) == null) {
            System.err.println("지정하신 화일이 없습니다");
            return null;
        } 
        else {
            try {
                src = Manager.createDataSource(mrl);
            }
            catch (IOException e) {
            } 
            catch (NoDataSourceException e) {      
            }
            return src;
        }
    }
    
    public void close() {
        synchronized (this) {
            player.close();
            while (!closed) {
                try {
                    wait(100);
                } catch (InterruptedException ie) {
                }
            }
        }
        player.removeControllerListener(this);
    }

        
    
    private void playStream() {
        
        
        if (player != null) {
            player.stop();
            player.deallocate();
            player.close();
            player = null;
        }
        
        try {
            // 지정된 미디어(mediaURL)를 위한 Player의 인스턴스를 생성시킨다.
            try {
                player = Manager.createPlayer(getDataSource());
            }
            catch (NoPlayerException e) {
                System.err.println("지정하신 화일이 없습니다");
            }
            // Add ourselves as a listener for a player's events
            //System.out.println("미디어를 시작시킨다.");
        } 
        catch (MalformedURLException e) {
            System.err.println("Invalid media file URL!");
        } 
        catch (IOException e) {
        }
    }
    public synchronized void wrapRealize()
    {
        player.realize();
        while(!realized)
        {
            try
            {
                wait();
                /*
                Processor p = null;
                DataSink sink = null;
                p = Manager.createProcessor(getDataSource());
                DataSource source = p.getDataOutput();

                MediaLocator dest = new MediaLocator("file:/C:/A.mpg");
                sink = Manager.createDataSink(source, dest);
                sink.open();
*/ } /*
            } catch (NoProcessorException e) {
                System.err.println(e);
            } catch (IOException e) {
                System.err.println(e);
            } catch (NoDataSinkException e) {
                System.err.println(e);
 
                 }  */
                catch (InterruptedException ie) {
            }
        }
    }
    public synchronized void controllerUpdate(ControllerEvent e) {
    
        if (e instanceof RealizeCompleteEvent) {
            realized = true;
            System.err.println("PURION_Player : RealizeCompleteEvet - 컴포넌트들을 넣는다.");
            player.setRate(rate);
            if ((controlComponent =
                    player.getControlPanelComponent()) != null) {
                pane.add(controlComponent, BorderLayout.SOUTH);
            }
            if ((visualComponent =
                    player.getVisualComponent()) != null) {
                pane.add(visualComponent, BorderLayout.CENTER);
                visualComponent.setBounds(0, 0, 100, 300);
            }
            if (controlComponent != null) {
                controlComponent.invalidate();
            }
        } else if (e instanceof PrefetchCompleteEvent) {
            player.start();
        } 
        else if (e instanceof EndOfMediaEvent) {
            
            player.stop();
            

            System.err.println("PURION_Player : RealizeCompleteEvet - 컴포넌트들을 넣는다.");
            System.out.println("EndOfMediaEvent.");
            player.setMediaTime(new Time(0));
            player.start();
        } 
        else if (e instanceof CachingControlEvent) {
            System.out.println("CachingControlEvent");
            player.setMediaTime(new Time(0));
            player.start();
        } 
        else if (e instanceof ControllerErrorEvent) {
            System.out.println("ControllerErrorEvent");
            //failed = true;
        }
        else if (e instanceof ControllerClosedEvent) {
            System.out.println("ControllerClosedEvent");
            closed = true;
        }
        notifyAll();
    }

    public void run() {
               
        this.playStream();
        player.addControllerListener(this);
        
        if(video) {
            this.setVisible(true);
        }
        else {
            this.setVisible(false);
        }
        
        wrapRealize();
        player.prefetch();
    }
}
